/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package utfpr.cp.extractor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import utfpr.cp.model.DAO;
import utfpr.cp.model.HTMLTag;
import utfpr.cp.model.HTMLTagDAO;
import utfpr.cp.model.ResourceData;
import utfpr.cp.model.ResourceDataDAO;
import utfpr.cp.model.URLData;


/**
 *
 * @author Adriano_2
 */
public class ParseHTMLTags {
    
    public ArrayList<HTMLTag> parse(URLData url, String html) {
        Document doc = Jsoup.parse(html);
        Iterator<Element> result = doc.select("*").iterator();
        ArrayList<HTMLTag> list = new ArrayList<>();
        while (result.hasNext()) {
            Element e = result.next();
            if (e.tagName().equals("#root")) continue;
            
            HTMLTag tag = new HTMLTag(url, e.tagName());
            Iterator<Attribute> attrs = e.attributes().iterator();
            while (attrs.hasNext()) {
                Attribute a = attrs.next();
                tag.addAttributos(a.getKey(), a.getValue());
            }
            
            list.add(tag);
        }
        return list;
    }

    public void run() {
        EntityManager em = DAO.getEntity();
        ResourceDataDAO resourceDAO = new ResourceDataDAO(em);
        HTMLTagDAO dao = new HTMLTagDAO(em);
        List<ResourceData> list = resourceDAO.getResoursesToParser();
        System.out.println("[HTML_PARSER] Iniciando a extração");
        while (!list.isEmpty()) {
            for (ResourceData res : list) {
                System.out.println("[HTML_PARSER] " + res.getUrl());
                ArrayList<HTMLTag> tags = parse(res.getUrl(), res.getContent());
                if (!dao.saveAll(tags)) {
                    System.out.println("   Falha ao salvar a Página " + res.getId());
                    try {
                        em.close();
                    }
                    catch (Exception e) {}
                    
                    em = DAO.getEntity();
                    resourceDAO = new ResourceDataDAO(em);
                    dao = new HTMLTagDAO(em);
                    break;
                }
            }
            list = resourceDAO.getResoursesToParser();
        }
        System.out.println("[HTML_PARSER] Extração concluida");
        em.close();
    }
}
